package com.edu.zju.scs.panacea.controller.resource;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.query.LocQuery;
import com.edu.zju.scs.panacea.entity.query.PositionQuery;
import com.edu.zju.scs.panacea.entity.resource.Loc;
import com.edu.zju.scs.panacea.entity.resource.Site;
import com.edu.zju.scs.panacea.service.resource.LocService;
import com.edu.zju.scs.panacea.utils.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-20 14:43
 */
@Controller
@EnableAutoConfiguration
public class LocController {

    @Autowired
    private LocService locService;

    @ResponseBody
    @ApiOperation(value="根据id查询某个资源位", notes="根据id查询某个资源位")
    @RequestMapping(value = "/loc/{id}", method = RequestMethod.GET)
    public BaseResult getLoc(@PathVariable Long id) {
        return locService.getLoc(id);
    }

    @ResponseBody
    @ApiOperation(value="查询某个站点下的所有资源位", notes="查询某个站点下的所有资源位")
    @RequestMapping(value = "/loc/site", method = RequestMethod.GET)
    public BaseResult getLocBySiteId(HttpSession session) {
        Site site = (Site) session.getAttribute("site");
        LocQuery locQuery = new LocQuery();
        locQuery.setSiteId(site.getId());
        BaseResult baseResult = locService.getSpecificLocs(locQuery);
        return baseResult;
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除资源位", notes="根据ID删除资源位")
    @RequestMapping(value = "/loc/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deleteLoc(@PathVariable Long id) {
        return locService.deleteLoc(id);
    }

    @ResponseBody
    @ApiOperation(value="新增资源位", notes="新增资源位")
    @RequestMapping(value = "/locs/new", method = RequestMethod.POST)
    public BaseResult addLoc(
            @ApiParam("名称") @RequestParam(value = "name") String name,
            @ApiParam("资源位高度") @RequestParam(value = "height") Integer height,
            @ApiParam("资源位宽度") @RequestParam(value = "width") Integer width,
            @ApiParam("所属大屏ID") @RequestParam(value = "devs") List<Long> devIds,
            @ApiParam("资源位模板ID列表") @RequestParam(value = "templates") List<Long> template) {
        List<Loc> locList = new ArrayList<>();
        for (Long devId : devIds) {
            Loc loc = new Loc();
            loc.setName(name);
            loc.setDevId(devId);
            loc.setHeight(height);
            loc.setWidth(width);
            locList.add(loc);
        }
        return locService.addLocs(locList, template);
    }

    @ResponseBody
    @ApiOperation(value="修改资源位信息", notes="修改资源位信息")
    @RequestMapping(value = "/loc/edit", method = RequestMethod.POST)
    public BaseResult editLoc(
            @ApiParam("ID") @RequestParam(value = "id") Long id,
            @ApiParam("名称") @RequestParam(value = "name") String name,
            @ApiParam("资源位高度") @RequestParam(value = "height") Integer height,
            @ApiParam("资源位宽度") @RequestParam(value = "width") Integer width,
            @ApiParam("所属大屏ID") @RequestParam(value = "devId") Long devId,
            @ApiParam("资源位模板ID列表") @RequestParam(value = "templates") List<Long> templates) {

        Loc loc = new Loc();
        loc.setId(id);
        loc.setName(name);
        loc.setHeight(height);
        loc.setWidth(width);
        loc.setDevId(devId);
        return locService.editLoc(loc, templates);
    }

    @ResponseBody
    @ApiOperation(value="删除某个设备的资源位", notes="删除某个设备的资源位")
    @RequestMapping(value = "/loc/deletion/dev/{devId}", method = RequestMethod.GET)
    public BaseResult deleteLocByDevId(@PathVariable Long devId) {
        BaseResult baseResult = locService.deleteLocByDev(devId);
        return baseResult;
    }


    @ResponseBody
    @ApiOperation(value="根据特定查询条件查询资源位", notes="根据特定查询条件查询资源位")
    @RequestMapping(value = "/locs/search", method = RequestMethod.POST)
    public BaseResult getSpecificLocs(
            @ApiParam("ID") @RequestParam(value = "locId", required = false) Long id,
            @ApiParam("名称") @RequestParam(value = "locName", required = false) String name,
            @ApiParam("省份") @RequestParam(value = "province", required = false) String province,
            @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
            @ApiParam("区/县") @RequestParam(value = "district", required = false) String district,
            @ApiParam("支持模板的类型") @RequestParam(value = "templateType", required = false) Integer templateType) {
        if (name != null && StringUtil.isTrimEmpty(name)) {
            name = null;
        }

        LocQuery locQuery = new LocQuery();
        locQuery.setId(id);
        locQuery.setName(name);
        locQuery.setProvince(province);
        locQuery.setCity(city);
        locQuery.setDistrict(district);
        locQuery.setTemplateType(templateType);
        System.out.println(locQuery.toString());

        return locService.getSpecificLocs(locQuery);
    }


    @ResponseBody
    @ApiOperation(value="查找资源位所在的位置列表", notes="查找资源位所在的位置列表")
    @RequestMapping(value = "/loc/positions/{type}", method = RequestMethod.POST)
    public BaseResult getLocProvinces(
            @PathVariable Integer type,
            @ApiParam("省份") @RequestParam(value = "province", required = false) String province,
            @ApiParam("城市") @RequestParam(value = "city", required = false) String city) {
        PositionQuery positionQuery = new PositionQuery();
        positionQuery.setType(type);
        positionQuery.setProvince(province);
        positionQuery.setCity(city);
        System.out.println(positionQuery.toString());
        return locService.getLocPositions(positionQuery);
    }

}
